﻿using System.Collections.Generic;
using System.Data;
using personalplaner.business.enums;
using personalplaner.business.stamm;
using System;
using personalplaner.business.common;

namespace personalplaner.dataprovider.services
{
	public partial class CommonDataService
	{
		private IEnumerable<OptionValue> m_optionseData(DataTable table, enOptionType optionType)
		{
			var wertExists = table.Columns.Contains("wert");
			var textwertExists = table.Columns.Contains("textwert");


			foreach (DataRow row in table.Rows)
			{
				yield return new OptionValue
				{
					ID = wertExists ? row.Field<int>("wert") : int.MaxValue,
					Name = optionType.ToString(),
					Wert = wertExists ? row.Field<int>("wert") : 0,
					Text = textwertExists ? row.Field<string>("textwert") : ""
				};
			}
		}

		private IEnumerable<PlanMonth> m_ActiveMonthsData(DataTable table)
		{
			var jahridExists = table.Columns.Contains("jahrid");
			var jahrExists = table.Columns.Contains("jahr");
			var nameExists = table.Columns.Contains("name");
			var vonExists = table.Columns.Contains("von");
			var bisExists = table.Columns.Contains("bis");
			var atidExists = table.Columns.Contains("atid");
			var tageExists = table.Columns.Contains("tage");
			var typwertExists = table.Columns.Contains("typwert");


			foreach (DataRow row in table.Rows)
			{
				yield return new PlanMonth
				{
					ID = int.MaxValue,
					Name = nameExists ? row.Field<string>("name") : string.Empty,
					JahrId = jahridExists ? row.Field<int>("jahrid") : 0,
					Jahr = jahrExists ? row.Field<int>("jahr") : 0,
					Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
					Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
					AtId = atidExists ? row.Field<int>("atid") : 0,
					Tage = tageExists ? Convert.ToDouble(row.Field<decimal>("tage")) : 0.00,
					TypWert = typwertExists ? row.Field<int>("typwert") : 0
				};
			}
		}

		internal static IEnumerable<Feiertag> m_FeiretageData(DataTable table)
		{
			var nameExists = table.Columns.Contains("name");
			var vonExists = table.Columns.Contains("von");
			var bisExists = table.Columns.Contains("bis");
			var jahridExists = table.Columns.Contains("jahrid");
			var atidExists = table.Columns.Contains("atid");
			var tageExists = table.Columns.Contains("tage");

			foreach (DataRow row in table.Rows)
			{
				yield return new Feiertag
				{
					ID = int.MaxValue,
					Name = nameExists ? row.Field<string>("name") : "Feiertag",
					Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
					Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
					JahrId = jahridExists ? row.Field<int>("jahrid") : 0,
					AtId = atidExists ? row.Field<int>("atid") : 0,
					Tage = tageExists ? Convert.ToDouble(row.Field<decimal>("tage")) : 0,
				};
			}
		}

		private IEnumerable<PlanYear> m_ExistingYearsData(DataTable table)
		{
			var jahridExists = table.Columns.Contains("jahrid");
			var jahrExists = table.Columns.Contains("jahr");
			var vonExists = table.Columns.Contains("von");
			var bisExists = table.Columns.Contains("bis");
			var statusExists = table.Columns.Contains("status");


			foreach (DataRow row in table.Rows)
			{
				yield return new PlanYear
				{
					ID = int.MaxValue,
                    Name = "planyear",
					JahrId = jahridExists ? row.Field<int>("jahrid") : 0,
					Jahr = jahrExists ? row.Field<int>("jahr") : 0,
					Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
					Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
					Status = statusExists ? row.Field<int>("status") == 1 ? enStatus.Active : enStatus.Inactive : enStatus.Inactive
				};
			}
		}

        private IEnumerable<Mandant> m_MandantenData(DataTable table)
        {
            var mandidExists = table.Columns.Contains("mandid");
            var nameExists = table.Columns.Contains("name");
            var beschreibungExists = table.Columns.Contains("beschreibung");
            var activeExists = table.Columns.Contains("active");


            foreach (DataRow row in table.Rows)
            {
                yield return new Mandant
                {
                    ID = int.MaxValue,
                    Name = nameExists ? row.Field<string>("name") : string.Empty,
                    MandId = mandidExists ? row.Field<string>("mandid") : string.Empty,
                    Beschreibung = beschreibungExists ? row.Field<string>("beschreibung") : string.Empty,
                    IsActive = activeExists ? row.Field<int>("active") == 1 : false,
                };
            }
        }
	}
}
